package org.hermes.core;

import java.util.ArrayList;
import java.util.Collection;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.hermes.core.message.ServiceMessage;


public class ServiceContext {
	private static final ThreadLocal<ServiceContext> threadLocal = new ThreadLocal<ServiceContext>();
	
	private HttpServletRequest request;
	private Collection<ServiceMessage> serviceMessages;

	public ServiceContext(){
		this.serviceMessages = new ArrayList<ServiceMessage>();
	}
	
	public HttpServletRequest getRequest() {
		return request;
	}
	
	public HttpSession getHttpSession(){
		return request.getSession();
	}
	
	public void addServiceMessage(ServiceMessage serviceMsg){
		serviceMessages.add(serviceMsg);
	}
	
	public Collection<ServiceMessage> getServiceMessages(){
		return serviceMessages;
	}
	
	public static ServiceContext newInstance(HttpServletRequest request){
		ServiceContext ctx = new ServiceContext();
		ctx.request = request;
		threadLocal.set(ctx);
		
		return ctx;
	}
	
	public static ServiceContext getCurrentServiceContext(){
		return threadLocal.get();
	}
	
	public static void destroy(){
		threadLocal.set(null);
	}
}